//
//  RJSONKit.m
//  project


#import "RJSONKit.h"


@implementation NSDictionary (RJSONKit)

-(NSString *)JSONString
{
    NSString * jsonString=nil;
    jsonString= [[NSString alloc]initWithData:[self JSONData] encoding:NSUTF8StringEncoding];
    
    return jsonString;
}
/**
 *  @brief NSDictionary转换成JSON字符串
 *
 *  @return  JSON字符串
 */
-(NSString *)jk_JSONString
{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    if (jsonData == nil) {
#ifdef DEBUG
        NSLog(@"fail to get JSON from dictionary: %@, error: %@", self, error);
#endif
        return nil;
    }
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return jsonString;
}

-(NSData *)JSONData
{
    NSError * error;
    
    NSData * data= [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error];
    //    DlogError(error);
    return data;
}


@end


@implementation NSString (RJSONKit)

-(NSDictionary *)objectFromJSONString
{
    NSError * error=nil;
    NSDictionary * jsonObject=nil;
    
    jsonObject= [NSJSONSerialization JSONObjectWithData:[self dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
//    DlogError(error);
    
    return jsonObject;
}
@end

@implementation NSData (RJSONKit)

-(NSDictionary *)objectFromJSONData
{
    NSError * error=nil;
    NSDictionary * jsonObject=nil;
    jsonObject =[NSJSONSerialization JSONObjectWithData:self options:NSJSONReadingAllowFragments error:&error];
//    DlogError(error);
    
    return jsonObject;
}


@end
